using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SocketClient
{
    public partial class FormClient : Form
    {
        public FormClient()
        {
            InitializeComponent();
        }

        Socket skClient = null;

        private void btnConn_Click(object sender, EventArgs e)
        {
            textIP.Enabled = false;
            numPort.Enabled = false;
            btnConn.Enabled = false;

            // 1.创建一个Socket
            if (skClient == null)
            {
                skClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            }

            // 2.连接对应服务器
            skClient.Connect(new IPEndPoint(IPAddress.Parse(textIP.Text), (int)numPort.Value));

            textLog.AppendText($"连接服务器【{textIP.Text}:{numPort.Value}】成功" + Environment.NewLine);

            Action<string> action = (string msg) =>
            {
                textLog.AppendText($"服务器发来消息：{msg}" + Environment.NewLine);
            };

            Action action2 = () =>
            {
                this.WindowState = FormWindowState.Normal;
                this.TopMost = true;
                Random rdm = new Random();
                Point p = this.Location;
                for (int i = 0; i < 50; i++)
                {
                    int x = rdm.Next(1, 6);
                    int y = rdm.Next(1, 6);
                    this.Location = new Point(p.X + x, p.Y + y);
                    Thread.Sleep(50);
                    this.Location = p;
                }
                this.TopMost = false;
            };

            ThreadPool.QueueUserWorkItem(new WaitCallback(obj =>
            {
                byte[] buffer = new byte[1024];
                while (true)
                {
                    if (skClient != null && skClient.Available > 0)
                    {
                        int r = skClient.Receive(buffer);
                        string msg = Encoding.UTF8.GetString(buffer, 0, r);
                        if (msg == "闪屏")
                        {
                            Invoke(action2);
                        }
                        Invoke(action, msg);
                    }
                }
            }));

            // 3.向服务器发送一个消息
            //string msg = "大家好，我是Vae";
            //byte[] buffers = System.Text.Encoding.UTF8.GetBytes(msg);
            //skClient.Send(buffers);
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            string msg = textSendMsg.Text;

            if (skClient != null && skClient.Connected)
            {
                byte[] buffer = System.Text.Encoding.UTF8.GetBytes(msg);
                skClient.Send(buffer);
                textLog.AppendText("向服务器发送消息：" + msg + Environment.NewLine);
                textSendMsg.Text = "";
            }
        }

        private void FormClient_FormClosing(object sender, FormClosingEventArgs e)
        {
            skClient.Shutdown(SocketShutdown.Both);
            skClient.Close();
            skClient.Dispose();
            skClient = null;
        }
    }
}
